package model.commands;

import ctf.model.Side;
import ctf.network.ProtocolError;
import model.GameConstants;
import model.IModel;

public class SpinPlayerCommand extends AbstractModelCommand {

	private Side side;
	private int playerNum;
	private float newAngularVelocity;

	public SpinPlayerCommand(int executionTimeInMs, Side side,
			int playerNum, float newAngularVelocity) {
		super(executionTimeInMs);
		this.side = side;
		this.playerNum = playerNum;
		this.newAngularVelocity = newAngularVelocity;
	}

	@Override
	public void checkValid(IModel model) throws ProtocolError {
		if (newAngularVelocity > GameConstants.MAXIMUM_PLAYER_ANGULAR_VELOCITY
				|| newAngularVelocity < GameConstants.MINIMUM_PLAYER_ANGULAR_VELOCITY) {
			throw new ProtocolError("Expected player acceleration in ("
					+ GameConstants.MINIMUM_PLAYER_ANGULAR_VELOCITY + ","
					+ GameConstants.MAXIMUM_PLAYER_ANGULAR_VELOCITY + ")",
					Float.toString(newAngularVelocity));
		}
		if (model.playerExists(side, playerNum) == false) {
			throw new ProtocolError("Expected player on side "
					+ side.toString(), "No player in side "
					+ side.toString());
		}

	}

	@Override
	public void execute(IModel model) {
		model.spinPlayer(this.side, this.playerNum, this.newAngularVelocity);
	}

}
